package Pemilik;

import Kasir.Kasir;
import Koneksi.Koneksi;
import java.sql.*;
import java.sql.Connection;
import java.util.*;

/**
 *
 * @author Astri
 */
public class tambahKasirKontrol {

    Connection konek;
    private static tambahKasirKontrol kasirKategori;
    private Kasir kasir;

    public tambahKasirKontrol(Connection konek) {
        this.konek = konek;
    }

    public static tambahKasirKontrol getKoneksiKontrol() {
        Koneksi k = new Koneksi();
        if (kasirKategori == null) {
            kasirKategori = new tambahKasirKontrol(k.getConnection());
        }
        return kasirKategori;
    }

    public void simpanKasirStatement(Kasir kasir) throws SQLException {
        String id = kasir.getId_Kasir();
        String nama = kasir.getNama_Kasir();
        String almt = kasir.getAlamat();
        String paswd = kasir.getPassword();
        String stt = kasir.getStatus();
        String sql = "insert into kasir(Id_Kasir,Nama_Kasir,Alamat,Password,Status)" 
                + "values('" + id + "','" + nama + "','" + almt + "','" + paswd + "','" + stt + "')";

        Statement stat = konek.createStatement();
        stat.executeUpdate(sql);
    }

    public List<Kasir> getAllKasir() throws SQLException {
        PreparedStatement prepare = null;
        ResultSet rs = null;


        String sql = "select Id_Kasir,Nama_Kasir,Alamat,Password,Status from kasir order by Id_Kasir";
        prepare = konek.prepareStatement(sql);
        rs = prepare.executeQuery();
        List<Kasir> kasir = new ArrayList<Kasir>();
        while (rs.next()) {
            Kasir ks = new Kasir();
            String id = rs.getString("Id_Kasir");
            String nama = rs.getString("Nama_Kasir");
            String almt = rs.getString("Alamat");
            String paswd = rs.getString("Password");
            String stt = rs.getString("Status");
            
            ks.setId_Kasir(id);
            ks.setNama_Kasir(nama);
            ks.setAlamat(almt);
            ks.setPassword(paswd);
            ks.setStatus(stt);
            kasir.add(ks);
        }
        konek.commit();
        return kasir;
    }    
    
    public Kasir cariKasir(Kasir kasir) throws SQLException{
        CallableStatement callst = null;
        
        String sql = "call cariKasir(?,?)";
        callst = konek.prepareCall(sql);
        callst.setString(1, kasir.getId_Kasir());
        callst.registerOutParameter(2, java.sql.Types.VARCHAR);
        callst.execute();
        
        String nama = callst.getString(2);
        callst.execute();
        kasir.setNama_Kasir(nama);
        return kasir;       
    }
}
